package com.example.qjia.basic;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.example.qjia.data.romotedata.Student;

import java.util.List;
import java.util.Set;

/**
 * Created by ying_xiang on 2016/8/10.
 */
public class MyApplication extends Application {
    public static MyApplication instance;

    @Override
    public void onCreate() {
        super.onCreate();
        this.instance=this;
    }

    public static MyApplication getInstance() {     //获取MyAppliction实例;
        return instance;
    }
    public static boolean isLogin(){
        SharedPreferences sharedPreferences=MyApplication.getInstance().getApplicationContext().getSharedPreferences("userdata", Context.MODE_PRIVATE);


        return sharedPreferences.getBoolean("loginState",false);
    }
    public static int getStudentId(){
        SharedPreferences sharedPreferences=MyApplication.getInstance().getApplicationContext().getSharedPreferences("studentMessage", Context.MODE_PRIVATE);
        return sharedPreferences.getInt("s_id",0);
    }
    public static void setLogin(){
        SharedPreferences sharedPreferences=MyApplication.getInstance().getApplicationContext().getSharedPreferences("studentMessage", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putBoolean("loginstate",true);
        editor.commit();


    }
    public static void setStudentMessage( Student student){
        SharedPreferences sharedPreferences=MyApplication.getInstance().getApplicationContext().getSharedPreferences("studentMessage", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();

        editor.putInt("s_id",student.getSid());
        editor.putString("studentName",student.getName());
        editor.putString("studentId",student.getStudentId());
        editor.putString("studentMail",student.getEmail());
        editor.putString("major",student.getMajor());
        editor.putString("majorClass",student.getMclass());
        editor.putString("acadamy",student.getAcademy());
        editor.putString("phone",student.getPhone());
        editor.commit();
    }
    public static void loginOut(){           //注销信息
        SharedPreferences sharedPreferences=MyApplication.getInstance().getApplicationContext().getSharedPreferences("userdata", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putBoolean("loginstate",false);
        editor.putInt("s_id",0);
        editor.putString("studentName",null);
        editor.putString("studentId",null);
        editor.putString("studentMail",null);
        editor.putString("major",null);
        editor.putString("majorClass",null);
        editor.putString("acadamy",null);
        editor.putString("phone",null);
        editor.commit();
    }
    public static Student getStudentMessage(){     //返回学生的基本信息
        Student student=new Student();
        SharedPreferences sharedPreferences=MyApplication.getInstance().getApplicationContext().getSharedPreferences("studentMessage", Context.MODE_PRIVATE);//创建一个名为studentMessage的xml文件
        student.setName(sharedPreferences.getString("name",null));
        student.setStudentId(sharedPreferences.getString("studentId",null));
        student.setAcademy(sharedPreferences.getString("acadamy",null));
        student.setMajor(sharedPreferences.getString("major",null));
        student.setEmail(sharedPreferences.getString("studentMail",null));
        student.setMclass(sharedPreferences.getString("marjorClass",null));
        student.setPhone(sharedPreferences.getString("phone",null));
        return student;
    }
}
